package com.jyb.qiyefuwu.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.jyb.qiyefuwu.api.EnterpriseServiceAPIManager;
import com.jyb.qiyefuwu.entity.ApplyForPaymentEntity;
import com.jyb.qiyefuwu.entity.DepartmentEntity;
import com.jyb.qiyefuwu.entity.NecessaryEntity;
import com.sgb.net.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;


import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ApplyForPaymentModel extends BaseViewModel {
    public MutableLiveData<String> dataText = new MutableLiveData<>();
    public MutableLiveData<String> examinationId = new MutableLiveData<>();
    public ApplyForPaymentEntity applyData = new ApplyForPaymentEntity();
    public NecessaryEntity necessaryEntity = new NecessaryEntity();
    public MutableLiveData<List<DepartmentEntity>> departmentData = new MutableLiveData<>();
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();


    private long midTime;
    private String info;

    public ApplyForPaymentModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 申请付款
     *
     * @param data
     */
    public void applyForPayment(ApplyForPaymentEntity data) {
        ShowLoading.postValue(1);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().applyForPayment(data).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                ShowLoading.postValue(2);
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
//                    mToast.postValue("申请提交成功");
                    examinationId.postValue((String) response.body().getData());
                }
//                else if("".equals(response.body().getMsg())){
//                    showdialog.postValue(1);
//                }
                else{
                    dataText.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                ShowLoading.postValue(2);
                String msg = t.getMessage();
            }
        });
    }

    /**
     * 申请付款成功后调用
     *
     * @param data
     */
    public void necessary(NecessaryEntity data) {
        ShowLoading.postValue(1);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().necessary(data).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {

                if (response.body() == null) {
                    return;
                }
                dataText.postValue(response.body().getMsg());
                if (response.body().getState().equals("ok")) {

                }
                ShowLoading.postValue(BaseViewModel.FINISH_AND_DISMISS_LODING);
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                ShowLoading.postValue(BaseViewModel.FINISH_AND_DISMISS_LODING);
            }
        });
    }

    /**
     * 部门列表
     *
     * @param companyNo
     */
    public void applyForPayment(String companyNo) {
        ShowLoading.postValue(1);
        EnterpriseServiceAPIManager.getInstance().getUserCenterAPI().getOrganizationByCompany("v1/app/enterpriseServices/order/getOrganizationByCompany/" + companyNo).enqueue(new Callback<BaseEntity<List<DepartmentEntity<List<DepartmentEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DepartmentEntity<List<DepartmentEntity>>>>> call, Response<BaseEntity<List<DepartmentEntity<List<DepartmentEntity>>>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    if (response.body().getData().get(0).getChildren() != null)
                        departmentData.postValue(response.body().getData().get(0).getChildren());
                } else {
                    dataText.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DepartmentEntity<List<DepartmentEntity>>>>> call, Throwable t) {
                ShowLoading.postValue(2);
                String msg = t.getMessage();
            }
        });
    }
}
